/*
 * @Author: zhangyang
 * @Date: 2022-02-24 15:55:05
 * @LastEditTime: 2022-02-24 17:19:10
 * @Description: 存储标签页的状态
 */
import { defineStore, acceptHMRUpdate } from 'pinia';
import type { RouteLocationNormalized } from 'vue-router';

type CachedView = string | symbol | null | undefined;

interface TagsViewState {
  /**
   * 访问过的页面，路由对象
   */
  visitedViews: RouteLocationNormalized[];
  /**
   * 已缓存的页面名称
   */
  cachedViews: CachedView[];
  /**
   * 当前激活的导航菜单
   */
  currentMenuIndex: string;
}
export const useTagsStore = defineStore('useTagsStore', {
  state: () => {
    const state = reactive<TagsViewState>({
      visitedViews: [],
      cachedViews: [],
      currentMenuIndex: '',
    });
    return state;
  },
  actions: {
    /**
     * 打开某个页面
     */
    addView(view: RouteLocationNormalized) {
      // 查询是否已经访问过
      if (!this.visitedViews.some((v) => v.path === view.path)) {
        // 添加到已访问
        this.visitedViews.push(deepClone(view));
      }
    },
    /**
     * 添加页面到缓存
     */
    addToCache(view: RouteLocationNormalized) {
      // 查询该标签是否已缓存
      if (this.cachedViews.includes(view.name)) {
        null;
      } else if (!view.meta?.noCache) {
        this.cachedViews.push(view.name);
      }
    },
    /**
     * 关闭某个页面
     */
    delView(view: RouteLocationNormalized) {
      // 删除访问记录
      for (const [i, v] of this.visitedViews.entries()) {
        if (v.path === view.path) {
          this.visitedViews.splice(i, 1);
          break;
        }
      }
      // 删除缓存
      this.delCachedView(view);
    },
    /**
     * 删除页面缓存
     */
    delCachedView(view: RouteLocationNormalized) {
      const index = this.cachedViews.indexOf(view.name);
      index > -1 && this.cachedViews.splice(index, 1);
    },
    /**
     * 关闭其他页面
     */
    delOtherViews(view: RouteLocationNormalized) {
      this.visitedViews = this.visitedViews.filter((v) => {
        return v?.meta?.affix || v.path === view.path;
      });
      this.cachedViews = this.cachedViews.filter((v) => {
        return v === view.name;
      });
    },
    /**
     * 关闭所有页面
     */
    delAllViews() {
      const affixTags = this.visitedViews.filter((tag) => tag?.meta?.affix);
      this.visitedViews = affixTags;
      this.cachedViews.length = 0;
    },
  },
});

import.meta.hot && import.meta.hot.accept(acceptHMRUpdate(useTagsStore, import.meta.hot));
